/* vim: set ts=2 et sw=2 cindent fo=qroca: */

package com.globant.google.mendoza.command;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.globant.google.mendoza.MendozaServer;
import com.globant.google.mendoza.MendozaRequest;
import com.globant.google.mendoza.MendozaServerState;
import com.globant.google.mendoza.malbec.Order;

/** Represents the mendoza cancel order command.
 */
public final class MendozaCancelOrderCommand extends MendozaBaseCommand {

  /** The class logger.
   */
  private static Log log = LogFactory.getLog(MendozaCancelOrderCommand.class);

  /** The reason label. */
  private final String reasonLabel = "Reason";

  /** The comment label. */
  private final String commentLabel = "Comment";

  /** The reason to cancel the order. */
  private String reason = "";

  /** The comment to cancel the order. */
  private String comment = "";

  /** Creates an instance of MendozaCancelOrderCommand.
   *
   * @param mendozaRequest The mendoza server request.
   *
   * @param mendoza The mendoza server.
   *
   * @param mendozaState The mendoza server state.
   */
  public MendozaCancelOrderCommand(
      final MendozaRequest mendozaRequest, final MendozaServer mendoza,
      final MendozaServerState mendozaState) {
    super(mendozaRequest, mendoza, mendozaState);
    setName("Cancel order");
  }

  /** Executes the mendoza server cancel order command.
   */
  public void execute() {
    log.trace("Entering execute");
    MendozaCommandResult result = new MendozaCommandResult();
    String commandResultMsg =
        "OK - Cancel order command sent to the mendoza server.";
    try {
      MendozaServerState state = this.getMendozaState();
      if (!state.isNewOrderNotificationReceived()
          || !state.isRiskInformationNotificationReceived()) {
        throw new RuntimeException(
            "The order can not be cancelled in its"
            + " current financial order state.");
      }
      setRequestParameters();
      Order order = this.getMendozaState().getOrder();
      order.cancel(reason, comment);
      result.setSuccess(commandResultMsg);
    } catch (RuntimeException e) {
      commandResultMsg = e.getMessage();
      result.setError(commandResultMsg);
    }
    setResult(result);
    logCommand(this);
    log.trace("Leaving execute");
  }

  /** Sets the cancel order command parameters.
   */
  private void setRequestParameters() {
    log.trace("Entering setRequestParameters");
    reason = getMendozaRequest().getRequestParameters().getProperty("reason");
    comment = getMendozaRequest().getRequestParameters().getProperty("comment");
    if (reason == null) {
      log.trace("Leaving setRequestParameters");
      throw new RuntimeException("The reason cannot be null");
    }
    if (comment == null) {
      comment = reason;
    }
    logRequestParameter(reasonLabel, reason);
    logRequestParameter(commentLabel, comment);
    log.trace("Leaving setRequestParameters");
  }
}
